package com.huawei.demo;

import com.huaweicloud.sdk.cfw.v1.CfwClient;
import com.huaweicloud.sdk.cfw.v1.model.ListEipResourcesRequest;
import com.huaweicloud.sdk.cfw.v1.model.ListEipResourcesResponse;
import com.huaweicloud.sdk.cfw.v1.model.EipResource;
import com.huaweicloud.sdk.cfw.v1.model.AddBlackWhiteListUsingPostRequest;
import com.huaweicloud.sdk.cfw.v1.model.AddBlackWhiteListUsingPostResponse;
import com.huaweicloud.sdk.cfw.v1.model.AddBlackWhiteListDto;
import com.huaweicloud.sdk.cfw.v1.model.ListBlackWhiteListsUsingGetRequest;
import com.huaweicloud.sdk.cfw.v1.model.ListBlackWhiteListsUsingGetResponse;
import com.huaweicloud.sdk.cfw.v1.model.ListAccessControlLogsRequest;
import com.huaweicloud.sdk.cfw.v1.model.ListAccessControlLogsResponse;
import com.huaweicloud.sdk.cfw.v1.model.UpdateBlackWhiteListUsingPutRequest;
import com.huaweicloud.sdk.cfw.v1.model.UpdateBlackWhiteListDto;
import com.huaweicloud.sdk.cfw.v1.model.UpdateBlackWhiteListUsingPutResponse;
import com.huaweicloud.sdk.cfw.v1.model.DeleteBlackWhiteListUsingDeleteRequest;
import com.huaweicloud.sdk.cfw.v1.model.DeleteBlackWhiteListUsingDeleteResponse;
import com.huaweicloud.sdk.cfw.v1.region.CfwRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;

public class BlackWhiteList {

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk);
        CfwClient client = CfwClient.newBuilder().withCredential(auth).withRegion(CfwRegion.valueOf("<REGION ID>")).build();
        try {
            /* 4.4.1 通过查询防护eip列表查询到一条防护eip的地址 */
            String publicEIp = queryEip(client);

            /* 4.4.2 添加一条将防护eip设置为目的地址、协议为TCP、端口为0-65535、地址类型为ipv4的黑白名单，并获取黑名单id */
            String blackWhiteListId = addBlackWhiteList(client, publicEIp);

            /* 4.4.3 通过查询黑名单列表获取黑名单id */
            queryBlackWhiteList(client);

            /* 4.4.4 查询访问控制日志,获得阻断的访问控制日志 */
            queryAccessControlLong(client, publicEIp);

            /* 4.4.5 更新黑白名单为一个非防护eip的值,其余不变 */
            updateBlackWhiteList(client, blackWhiteListId);

            /* 4.4.6 删除黑白名单 */
            deleteBlackWhiteList(client, blackWhiteListId);
        } catch (ConnectionException e) {
            System.out.println(e.getMessage());
        } catch (RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void deleteBlackWhiteList(CfwClient client, String blackWhiteListId) {
        DeleteBlackWhiteListUsingDeleteRequest deleteBlackWhiteListUsingDeleteRequest
            = new DeleteBlackWhiteListUsingDeleteRequest();
        deleteBlackWhiteListUsingDeleteRequest.setListId(blackWhiteListId);
        DeleteBlackWhiteListUsingDeleteResponse deleteBlackWhiteListUsingDeleteResponse
            = client.deleteBlackWhiteListUsingDelete(deleteBlackWhiteListUsingDeleteRequest);
        System.out.println(deleteBlackWhiteListUsingDeleteResponse.toString());
    }

    private static void updateBlackWhiteList(CfwClient client,String blackWhiteListId) {
        UpdateBlackWhiteListUsingPutRequest updateBlackWhiteListUsingPutRequest = new UpdateBlackWhiteListUsingPutRequest();
        updateBlackWhiteListUsingPutRequest.setListId(blackWhiteListId);
        UpdateBlackWhiteListDto updateBlackWhiteListDto = new UpdateBlackWhiteListDto();
        updateBlackWhiteListDto.setAddress("1.1.1.1");
        updateBlackWhiteListDto.setAddressType(0);
        updateBlackWhiteListDto.setDirection(1);
        updateBlackWhiteListDto.setPort("0-65535");
        updateBlackWhiteListDto.setProtocol(6);
        updateBlackWhiteListDto.setListType(UpdateBlackWhiteListDto.ListTypeEnum.NUMBER_4);
        updateBlackWhiteListDto.setObjectId("<YOUR ObjectId>");
        updateBlackWhiteListUsingPutRequest.setBody(updateBlackWhiteListDto);
        UpdateBlackWhiteListUsingPutResponse updateBlackWhiteListUsingPutResponse = client.updateBlackWhiteListUsingPut(updateBlackWhiteListUsingPutRequest);
        System.out.println(updateBlackWhiteListUsingPutResponse.toString());
    }

    private static void queryAccessControlLong(CfwClient client,String publicEIp) {
        ListAccessControlLogsRequest listAccessControlLogsRequest = new ListAccessControlLogsRequest();
        listAccessControlLogsRequest.setDstIp(publicEIp);
        listAccessControlLogsRequest.setFwInstanceId("<YOUR FirewallInstanceId>");
        listAccessControlLogsRequest.setStartTime(1670427589817L);
        listAccessControlLogsRequest.setEndTime(1670431189817L);
        listAccessControlLogsRequest.setLimit(10);
        ListAccessControlLogsResponse listAccessControlLogsResponse = client.listAccessControlLogs(listAccessControlLogsRequest);
        System.out.println(listAccessControlLogsResponse.toString());
    }

    private static String queryBlackWhiteList(CfwClient client) {
        ListBlackWhiteListsUsingGetRequest listBlackWhiteListsUsingGetRequest = new ListBlackWhiteListsUsingGetRequest();
        listBlackWhiteListsUsingGetRequest.setObjectId("<YOUR ObjectId>");
        listBlackWhiteListsUsingGetRequest.setListType(ListBlackWhiteListsUsingGetRequest.ListTypeEnum.NUMBER_4);
        listBlackWhiteListsUsingGetRequest.setOffset(0);
        listBlackWhiteListsUsingGetRequest.setLimit(10);
        ListBlackWhiteListsUsingGetResponse listBlackWhiteListsUsingGetResponse = client.listBlackWhiteListsUsingGet(listBlackWhiteListsUsingGetRequest);
        String listId = listBlackWhiteListsUsingGetResponse.getData().getRecords().get(0).getListId();
        System.out.println(listId);
        return listId;
    }

    private static String addBlackWhiteList(CfwClient client,String publicEIp) {
        AddBlackWhiteListUsingPostRequest addBlackWhiteListUsingPostRequest = new AddBlackWhiteListUsingPostRequest();
        AddBlackWhiteListDto addBlackWhiteListDto = new AddBlackWhiteListDto();
        addBlackWhiteListDto.setListType(4);
        addBlackWhiteListDto.setObjectId("<YOUR ObjectId>");
        addBlackWhiteListDto.setDirection(1);
        addBlackWhiteListDto.setAddress(publicEIp);
        addBlackWhiteListDto.setProtocol(6);
        addBlackWhiteListDto.setPort("0-65535");
        addBlackWhiteListDto.setAddressType(0);
        addBlackWhiteListUsingPostRequest.setBody(addBlackWhiteListDto);
        AddBlackWhiteListUsingPostResponse addBlackWhiteListUsingPostResponse = client.addBlackWhiteListUsingPost(addBlackWhiteListUsingPostRequest);
        String blackWhiteListId = addBlackWhiteListUsingPostResponse.getData().getId();
        System.out.println(blackWhiteListId);
        return blackWhiteListId;
    }

    private static String queryEip(CfwClient client) {
        ListEipResourcesRequest listEipResourcesRequest = new ListEipResourcesRequest();
        listEipResourcesRequest.setObjectId("<YOUR ObjectId>");
        listEipResourcesRequest.setLimit(10);
        listEipResourcesRequest.setOffset(0);
        listEipResourcesRequest.setSync(ListEipResourcesRequest.SyncEnum.NUMBER_1);
        ListEipResourcesResponse listEipResourcesResponse = client.listEipResources(listEipResourcesRequest);
        EipResource eipResource = listEipResourcesResponse.getData().getRecords().get(0);
        String publicEIp = eipResource.getPublicIp();
        System.out.println(publicEIp);
        return publicEIp;
    }
}
